import usart from "@ohos.multimedia.usart";
import {UsartYielder} from"./Usart_yielder.js";
import {ParameterPack,CommunicationPack,Protocol} from "./MessagePack.js";
const str_port = 0;
function test_open(){
    var res= usart.isOpen(str_port);
    if (res){
        console.info("[Wang] /dev/ttyAMA0 is open");
    }
    else{
        console.error("[Wang] /dev/ttyAMA0 is NOT OPEN !!!");
    }
    return res;
}

function Uint8ArrayToString(fileData){
    if(!( fileData instanceof Uint8Array)){
        console.error("[Wang] Value is NOT a Uint8Array !!!")
    }
    var dataString = "";
    for (var i = 0; i < fileData.length; i++) {
        dataString += String.fromCharCode(fileData[i]);
    }

    return dataString
}


function stringToUint8Array(str){
    var arr = [];
    for (var i = 0, j = str.length; i < j; ++i) {
        arr.push(str.charCodeAt(i));
    }

    var tmpUint8Array = new Uint8Array(arr);
    return tmpUint8Array
}

var sleep = function(time_millisecond) {
    var startTime = new Date().getTime() + parseInt(time_millisecond, 10);
    while(new Date().getTime() < startTime) {}
};


export default {
    onStart(want) {
        console.info('[Wang] ServiceAbility onStart');
        if(usart == null){
            console.error("[Wang] usart module is not loaded !!!");
        }
        else{
            console.info("[Wang] usart module loaded,usart.open is " + usart.open.toString())
        }
        console.info("[Wang] trying to open /dev/ttyAMA0");
        usart.open(str_port);
        test_open();
        console.info("[Wang] initialize UsartYielder");
        var yielder= new UsartYielder(str_port);

        if(!usart.isOpen(str_port)){
            console.error("[Wang] /dev/ttyAMA0 is NOT OPEN !!!");
        }
        console.info("[Wang] send a CommunicationPack");
        console.info("[Wang] here is a wifi_connection message like \"wificonnect,wifipassword\"");
        var wifipack = Protocol.WifiConnect("a_wifi","a_password");
        console.info("[Wang] trans it to a u8array");
        const wifipack_u8array = wifipack.toU8Array();
        console.info(wifipack_u8array);
        console.info("[Wang] now I send it");
        usart.write(str_port,wifipack_u8array);
        console.info("[Wang] return value is a uint16 taking 2 bytes");
        var read_res_as_array=yielder.getbyte(2);
        const num=read_res_as_array[0]<<8 + read_res_as_array[1];
        console.info("[Wang] I get",num);
        console.info("[Wang] Here I end the Writing Loop Downwards",num);
        usart.endByteQueueWriting(str_port);
        return {};
    },
    onStop() {
        console.info('[Wang] ServiceAbility onStop');
    },
    onConnect(want) {
        console.info('[Wang] ServiceAbility onConnect');
        return {};
    },
    onReconnect(want) {
        console.info('[Wang] ServiceAbility onReconnect');
    },
    onDisconnect() {
        console.info('[Wang] ServiceAbility onDisconnect');
    },
    onCommand(want, restart, startId) {
        console.info('[Wang] ServiceAbility onCommand');
    }
};
//export default {
//    onCreate() {
//        console.info("[Wang] Application onCreate");
//
//        if(usart == null){
//            console.error("[Wang] usart module is not loaded !!!");
//        }
//        else{
//            console.info("[Wang] usart module is loaded");
//            console.info(usart.toString());
//            console.info("usart.open is ",usart.open.toString())
//        }
//        console.info("[Wang] trying to open /dev/ttyAMA0");
//        usart.open(str_port);
//        test_open();
//        console.info("[Wang] begin reading");
//        while(true){
//            if(!usart.isOpen(str_port)){
//                console.error("[Wang] /dev/ttyAMA0 is NOT OPEN !!!");
//            }
//            //一次读一行
//            const read_res_as_u8array=usart.read(str_port,8);
//            const read_res_as_string=Uint8ArrayToString(read_res_as_u8array);
//            console.info("[Wang] read :",read_res_as_string)
//            sleep(2000); // 延时函数，单位ms
//        }
//    },
//    onDestroy() {
//        console.info("[Wang] Application onDestroy");
//    }
//};
